﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FCTC_Web.Models;
using FCTC_Web.DAL;
using DAL;
using FCTC_Web.Utilities;

namespace FCTC_Web.Controllers.Maintains
{ 
    public class CaseController : Controller
    {
        //private FCTC_DataContext db = new FCTC_DataContext();

        //
        // GET: /Case/

        public ViewResult Index(int? pageId)
        {
            return View("Index", Pager6.Ins.GetPage<Case>(pageId));
        }
         

        //
        // GET: /Case/Details/5

        public ViewResult Details(int id)
        {
            Case casemodel = CH.GetDataById<Case>(id);
            return View(casemodel);
        }

        //
        // GET: /Case/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Case/Create

        [HttpPost]
        public ActionResult Create(Case casemodel)
        {
            if (ModelState.IsValid)
            {
                casemodel.UpdateTime = DateTime.Now;
                casemodel.UpdatedUser = HttpContext.User.Identity.Name;
                CH.Create<Case>(casemodel);
                var pageId = Pager6.Ins.GetPageId<Case>(casemodel.ID);
                return RedirectToAction("Index", new { pageId = pageId });
            }
            ViewBag.EstimatorID = DrapDownListHelper.InitialSelectionDropDownList<Estimator>("ID", "ID", "FullName");
            return View(casemodel);
        }
        
        //
        // GET: /Case/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View(CH.GetDataById<Case>(id));
            //Case casemodel = db.Cases.Find(id);
            //ViewBag.EstimatorID = new SelectList(db.Estimators, "ID", "CertificationNumber", casemodel.EstimatorID);
            //return View(casemodel);
        }

        //
        // POST: /Case/Edit/5

        [HttpPost]
        public ActionResult Edit(Case casemodel)
        {
            if (ModelState.IsValid)
            {
                CH.Edit<Case>(casemodel);
                var pageId = Pager6.Ins.GetPageId<Case>(casemodel.ID);
                return RedirectToAction("Index", new { pageId = pageId });
            }
            ViewBag.EstimatorID = DrapDownListHelper.InitialSelectionDropDownList<Estimator>("ID", "ID", "FullName");
            return View(casemodel);

            //if (ModelState.IsValid)
            //{
            //    db.Entry(casemodel).State = EntityState.Modified;
            //    db.SaveChanges();
            //    return RedirectToAction("Index");
            //}
            //ViewBag.EstimatorID = new SelectList(db.Estimators, "ID", "CertificationNumber", casemodel.EstimatorID);
            //return View(casemodel);
        }

        //
        // GET: /Case/Delete/5
 
        public ActionResult Delete(int id)
        {
            Case casemodel = CH.GetDataById<Case>(id);
            return View(casemodel);
        }

        //
        // POST: /Case/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            //Case casemodel = db.Cases.Find(id);
            //db.Cases.Remove(casemodel);
            //db.SaveChanges();
            //return RedirectToAction("Index");

            CH.Delete<Case>(id);
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }
    }
}